<?php

class Dni extends Module
{
    private $_html = '';
    private $_postErrors = array();

	function __construct()
	{
		$this->name = 'dni';
		$this->tab = 'Customers';
		$this->version = '0.6';

		parent::__construct();

		/* The parent construct is required for translations */
		$this->page = basename(__FILE__, '.php');
		$this->displayName = $this->l('Tax Identification Number');
		$this->description = $this->l('Allow the information of the customers\' Tax Identification Number');
	}
	
	public function getContent()
	{
		$output = '<h2>'.$this->displayName.'</h2>';
		if (Tools::isSubmit('submitDniNeeded'))
		{
			$dni = Tools::getValue('dni');
			if ($dni != 0 AND $dni != 1) 
				$output .= '<div class="alert error">'.$this->l('Field required is mandatory.').'</div>';
			else
			{
				Configuration::updateValue('DNI_NEEDED', $dni);
				$output .= '<div class="conf confirm"><img src="../img/admin/ok.gif" alt="'.$this->l('Settings updated').'" />'.$this->l('Settings updated').'</div>';
			}
		}
		return $output.$this->mandatory().$this->displayForm();
	}

	public function displayForm()
	{
		return '
		<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
			<fieldset>
				<legend><img src="'.$this->_path.'dni.gif" alt="" title="" />'.$this->l('Settings').'</legend>
				
				<label>'.$this->l('Field required:').'</label>
				<div class="margin-form">
					<input type="radio" name="dni" id="dni_on" value="1" style="vertical-align: middle;"'.(Tools::getValue('dni', Configuration::get('DNI_NEEDED')) ? 'checked="checked" ' : '').'/>
					<label class="t" for="dni_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Yes').'" title="'.$this->l('Yes').'" /></label>
					<input type="radio" name="dni" id="dni_off" value="0" style="vertical-align: middle;"'.(!Tools::getValue('dni', Configuration::get('DNI_NEEDED')) ? 'checked="checked" ' : '').'/>
					<label class="t" for="dni_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('No').'" title="'.$this->l('No').'" /></label>
					<p class="clear">'.$this->l('Do mandatory the information of the Tax Identification Number of the customers (compatible with the default theme)').'</p>
				</div>
				
				<center><input type="submit" name="submitDniNeeded" value="'.$this->l('Update settings').'" class="button" /></center>
			</fieldset>
		</form>';
	}

	public function mandatory()
	{
		$dnivalue = Configuration::get('DNI_NEEDED');
		if ($dnivalue == 1) {require(dirname(__FILE__).'/mandatory.php');
		}
		if ($dnivalue == 0) {require(dirname(__FILE__).'/nomandatory.php');
		}
	}

	function install()
	{
			if
			(
				parent::install() == false
				OR Configuration::updateValue('DNI_NEEDED', 1) == false
			)
			return false;

			/* Create back copies of the necessary fields and install modificated fields */
			require(dirname(__FILE__).'/install.php');
			
			/* Create the field dni into the customer table */
			$dni= mysql_query('SELECT `dni` FROM `'._DB_PREFIX_.'customer`');
			if (!$dni)
			{
			Db::getInstance()->Execute('ALTER TABLE `'._DB_PREFIX_.'customer` ADD `dni` VARCHAR(32) NOT NULL');
			}
			
			/* Fill dni field if it's empty */
			$query='SELECT * FROM `'._DB_PREFIX_.'customer`';
			$cust = mysql_num_rows (Db::getInstance()->Execute($query));
			$resul=mysql_query($query);
			for ($num = 1; $num <= $cust; $num ++)
			{
			$get_dni = mysql_result($resul,$num-1,'dni');
			if (empty($get_dni)) Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'customer` SET `dni` = \'NO ID\' WHERE `id_customer`='.$num.'');
			}
		return true;
	}

	function uninstall()
	{
		/* Uninstall the module, delete the field DNI_NEEDED from the configuration table
		   without deleting the field dni from the customer table */
		require(dirname(__FILE__).'/uninstall.php');
			
		return parent::uninstall()
		and Configuration::deleteByName('DNI_NEEDED')
		and unlink ($src21) and unlink ($src24) and unlink ($src25) and unlink ($src26) and unlink ($src27) and unlink ($src28) and unlink ($src29) and unlink ($src31);
	}
}
?>
